<template>
  <div>
    <!-- 卡片 -->
    <el-card class="box-card" style="margin-top: 10px">
      <!-- 标语 -->
      <el-alert
        title="注意:只允许为第三分类设置相关参数"
        type="warning"
        show-icon
      >
      </el-alert>
      <!-- 商品分类 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-cascader
          v-model="value"
          :options="options"
          :props="props"
          @change="handleChange"
        ></el-cascader>
      </el-form>
      <!-- tab切换 -->
      <el-tabs v-model="activeName" style="margin-top: 20px">
        <el-tab-pane label="动态参数" name="many">
          <!-- 按钮 -->
          <el-button
            type="primary"
            class="add"
            @click="dialogFormVisible1 = true"
            :disabled="this.value.length !== 0 ? false : true"
            >添加参数</el-button
          >
          <!-- 表格 -->
          <el-table :data="tableData" style="width: 100%">
            <!-- 箭头 -->
            <el-table-column type="expand" width="50">
              <template slot-scope="scope">
                <el-tag
                  :key="index"
                  v-for="(item, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag)"
                >
                  {{ item }}
                </el-tag>
                <!-- 显示添加小参数 -->
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <!-- 索引 -->
            <el-table-column type="index" width="50" label="#">
            </el-table-column>
            <!-- 参数名称 -->
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <!-- 操作 -->
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="danger"
                  size="mini"
                  @click="handleEdit(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- 表格 -->
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <!-- 按钮 -->
          <el-button
            type="primary"
            class="add"
            @click="dialogFormVisible2 = true"
            :disabled="this.value.length !== 0 ? false : true"
            >添加属性</el-button
          >
          <!-- 表格 -->
          <el-table :data="tableData1" style="width: 100%">
            <!-- 箭头 -->
            <el-table-column type="expand" width="50">
              <template slot-scope="scope">
                <!-- 小按钮 -->
                <el-tag
                  :key="tag"
                  v-for="tag in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(tag)"
                >
                  {{ tag }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="inputValue1"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="$event.target.blur()"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <!-- 索引 -->
            <el-table-column type="index" width="50" label="#">
            </el-table-column>
            <!-- 参数名称 -->
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <!-- 操作 -->
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="danger"
                  size="mini"
                  @click="handleEdits(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- 表格 -->
        </el-tab-pane>
      </el-tabs>

      <!-- 表格 -->
      
      <!-- 表格 -->
      <!-- 动态修改对话框 -->
      <el-dialog title="修改动态参数" :visible.sync="dialogFormVisible">
        <!-- 参数 -->
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="修改参数" prop="attr_name">
            <el-input v-model="ruleForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAttr">确定修改</el-button>
        </div>
      </el-dialog>

      <!-- 动态添加修改框 -->
      <el-dialog title="添加参数" :visible.sync="dialogFormVisible1">
        <!-- 参数 -->
        <el-form
          :model="ruleForm1"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="添加参数" prop="attr_name">
            <el-input v-model="ruleForm1.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="AttrAdd">确定添加</el-button>
        </div>
      </el-dialog>
      <!-- 静态添加参数 -->
      <el-dialog title="添加参数" :visible.sync="dialogFormVisible2">
        <!-- 参数 -->
        <el-form
          :model="ruleForm2"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="添加参数" prop="attr_name">
            <el-input v-model="ruleForm2.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="StaticAdd">确定添加</el-button>
        </div>
      </el-dialog>
      <!-- 静态修改对话框 -->
      <el-dialog title="修改静态参数" :visible.sync="dialogFormVisible3">
        <!-- 参数 -->
        <el-form
          :model="ruleForm3"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="修改参数" prop="attr_name">
            <el-input v-model="ruleForm3.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible3 = false">取 消</el-button>
          <el-button type="primary" @click="editAttrs">确定修改</el-button>
        </div>
      </el-dialog>
    </el-card>
    <!-- 卡片 -->
  </div>
</template>

<script>
import {
  GoodParamsList,
  GoodList,
  ParamDel,
  EditParams,
  ParamsAdd,
  ParamsAddEdit,
} from "@/request/api.js";
export default {
  data() {
    return {
      //input框值
      inputValue: "",
      inputValue1: "",
      //显示小参数
      inputVisible: false,
      //动态编辑对话框状态
      dialogFormVisible: false,
      //动态添加对话框状态
      dialogFormVisible1: false,
      //静态添加对话框状态
      dialogFormVisible2: false,
      //静态编辑对话框状态
      dialogFormVisible3: false,
      //修改数据
      ruleForm: {
        attr_name: "",
        attr_id: "",
      },
      // 动态添加数据
      ruleForm1: {
        attr_name: "",
        attr_id: "",
      },
      //静态添加数据
      ruleForm2: {
        attr_name: "",
        attr_id: "",
      },
      //静态编辑数据
      ruleForm3: {
        attr_name: "",
        attr_id: "",
      },
      //数据
      value: [],
      //练级数据
      props: {
        value: "cat_id",
        children: "children",
        label: "cat_name",
        expandTrigger: "hover",
      },
      options: [],
      //谁先谁后
      activeName: "many",
      // activeName1: "scoend",
      //表格1
      tableData: [],
      tableData1: [],
      //规则
      rules: {
        attr_name: [{ required: true, message: "请输入参数", trigger: "blur" }],
      },
    };
  },
  methods: {
    //小参数点击事件
    handleInputConfirm(row) {
      console.log(row);
      row.inputVisible = false;
      //判断输入的内容合法不合法
      // if (this.inputValue.length == 0) {
      //   return;
      // }
      //如果输入的内容合法就做后续处理
      row.attr_vals.push(this.inputValue);
      let params = {
        // id: this.value[2],
        attr_name: row.attr_name,
        attr_sel: row.attr_sel,
        attr_vals: row.attr_vals.join(","),
      };
      console.log(params);
      ParamsAddEdit(params).then((res) => {
        // console.log(this.ruleForm.attr_id,params)
        console.log(res);
        // this.handleChange();
        // this.$message({
        //   message: "编辑参数成功",
        //   type: "success",
        // });
        // row.inputVisible = false
        // row.inputValue = ''
      });
    },
    //显示小参数
    showInput(row) {
      row.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    //动态添加参数
    AttrAdd() {
      let params = {
        id: this.value[2],
        attr_name: this.ruleForm1.attr_name,
        attr_sel: "many",
      };
      // console.log(params);
      ParamsAdd(params).then((res) => {
        console.log(res);
        this.handleChange();
        this.dialogFormVisible1 = false;
        this.$message({
          message: "添加参数成功",
          type: "success",
        });
      });
    },
    //静态添加参数
    StaticAdd() {
      let params = {
        id: this.value[2],
        attr_name: this.ruleForm2.attr_name,
        attr_sel: "only",
      };
      // console.log(params);
      ParamsAdd(params).then((res) => {
        console.log(res);
        this.handleChange();
        this.dialogFormVisible2 = false;
        this.$message({
          message: "添加参数成功",
          type: "success",
        });
      });
    },
    //编辑操作
    handleEdits(row) {
      this.dialogFormVisible3 = true;
      this.ruleForm3.attr_id = row.attr_id;
      this.ruleForm3.attr_name = row.attr_name;
    },
    //静态编辑修改
    editAttrs() {
      let params = {
        id: this.value[2],
        attrId: this.ruleForm3.attr_id,
        attr_name: this.ruleForm3.attr_name,
        attr_sel: "only",
      };
      EditParams(params).then((res) => {
        // console.log(this.ruleForm.attr_id,params)
        // console.log(res);
        this.handleChange();
        this.$message({
          message: "编辑参数成功",
          type: "success",
        });
        this.dialogFormVisible3 = false;
      });
    },
    //关闭标签
    handleClose() {},
    //请求数据
    goods() {
      let type = [1, 2, 3];
      GoodParamsList(type).then((res) => {
        // console.log(res);
        this.options = res.data.data;
      });
    },
    //tab切换
    handleClick(tab, event) {
      // console.log(tab, event);
    },
    //商品参数编辑
    editAttr() {
      let params = {
        id: this.value[2],
        attrId: this.ruleForm.attr_id,
        attr_name: this.ruleForm.attr_name,
        attr_sel: "many",
      };
      EditParams(params).then((res) => {
        // console.log(this.ruleForm.attr_id,params)
        // console.log(res);
        this.handleChange();
        this.$message({
          message: "编辑参数成功",
          type: "success",
        });
        this.dialogFormVisible = false;
      });
    },
    //操作
    handleEdit(row) {
      this.dialogFormVisible = true;
      this.ruleForm.attr_id = row.attr_id;
      this.ruleForm.attr_name = row.attr_name;
    },
    //删除
    handleDelete(attr_id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          ParamDel(this.value[2], attr_id).then((res) => {
            // console.log(res);
            this.handleChange();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleChange(value) {
      // console.log(value);
      // console.log(this.value);
      let params = {
        id: this.value[2],
        sel: "many",
      };
      GoodList(params).then((res) => {
        // console.log(res);
        res.data.data.forEach((item) => {
          item.attr_vals =
            item.attr_vals.length == 0 ? [] : item.attr_vals.split(",");
          item.inputVisible = false;
        });
        this.tableData = res.data.data;
      });

      let params1 = {
        id: this.value[2],
        sel: "only",
      };
      GoodList(params1).then((res) => {
        // console.log(res);
        res.data.data.forEach((item) => {
          item.attr_vals = item.attr_vals.split(",");
          item.inputVisible = false;
        });
        this.tableData1 = res.data.data;
      });
    },
  },
  created() {
    this.goods();
    this.handleChange();
  },
  mounted() {},
  components: {},
  computed: {},
  watch: {},
};
</script>

<style lang='scss' scoped>
.demo-form-inline {
  text-align: left;
  margin-top: 10px;
}
.el-tag {
  margin: 10px;
}
.add {
  margin-right: 1200px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
